﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Customer_Customer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] != null)
        {
            btnRedirect.Visible = true;
            BtnLogin.Visible = false;
            BtnLogout.Visible = true;
            BtnOrder.Visible = true;
        }
        else
        {
            BtnOrder.Visible = false;
            btnRedirect.Visible = false;
            BtnLogin.Visible = true;
            BtnLogout.Visible = false;
        }
    }
    private bool isAllFieldRequire() 
    {
        if(string.IsNullOrWhiteSpace(txtName.Text)||
            string.IsNullOrWhiteSpace(txtPhone.Text)||
            string.IsNullOrWhiteSpace(txtAddress.Text))
        {
            return false;
        }
        return true;
    }
    private bool isRightPhone() 
    {
        try
        {
            int.Parse(txtPhone.Text);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!isAllFieldRequire()) 
        {
            lblWarn.Text = "All Field Required";
            return;
        }
        if (!isRightPhone()) 
        {
            lblWarn.Text = "Is not a right phone number";
            return;
        }
        
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        int i = (int)Session["OID"];
        string sql = "insert into _Customer values ({0},'{1}','{2}','{3}')";
        sql = string.Format(sql,i,txtName.Text,txtAddress.Text,txtPhone.Text);
        SqlCommand cmd = new SqlCommand(sql, myCon);
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
        }
        myCon.Close();
        Response.Redirect("http://nganluong.vn");
    }
    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        clsLogIn a = (clsLogIn)(Session["Username"]);

        if (a.IsAdmin)
        {
            Response.Redirect("../User Information/Information.aspx");
        }
        else
        {
            Response.Redirect("../Add Products/AddProducts.aspx");
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Login/Login.aspx");

    }
    protected void BtnLogout_Click1(object sender, EventArgs e)
    {
        Session["Username"] = null;
        Response.Redirect("../Login/Login.aspx");
    }
    protected void BtnOrder_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Order/Default.aspx");
    }
}